<template>
  <section class="wrapper">
    <div class="row fanmargin">
      <div class="col-lg-12">
        <h4 class="myh4"><i class="fa fa-angle-right"></i>  添加盘管</h4>
        <div class="form-panel">
          <div class=" form">
            <form class="cmxform form-horizontal style-form">
              <div class="form-group ">
                <label for="curl" class="control-label col-lg-2 fanfont">学校名称</label>
                <div class="col-lg-10">
                  <input class="form-control" v-model.trim="schoolName" type="text" required="" autofocus>
                </div>
              </div>
              <div class="form-group ">
                <label for="cname" class="control-label col-lg-2 fanfont">编号</label>
                <div class="col-lg-10">
                  <input class=" form-control" v-model.trim="schoolNumber" type="number" required="" >
                </div>
              </div>
              <div class="form-group ">
                <label for="curl" class="control-label col-lg-2 fanfont">通道名称</label>
                <div class="col-lg-10">
                  <input class="form-control" v-model.trim="channelName" type="text" required="">
                </div>
              </div>
              <div class="form-group ">
                <label for="ccomment" class="control-label col-lg-2 fanfont">描述</label>
                <div class="col-lg-10">
                  <textarea class="form-control" v-model.trim="information"></textarea>
                </div>
              </div>
              <div class="form-group">
                <div class="col-lg-offset-2 col-lg-10">
                  <button class="btn btn-theme"  v-bind:disabled="isReflash" type="button" @click="submit">保存</button>
                  <button class="btn btn-theme04" type="button" @click="cancel">返回</button>
                </div>
              </div>
            </form>
          </div>
        </div>
      </div>
    </div>
  </section>
</template>

<script>
  var numReg = /^[0-9]+$/
  var numRe = new RegExp(numReg)
  export default {
    name: 'addschool',
    data () {
      return {
        isReflash:false,
        schoolName:'',
        information:'',
        schoolNumber:'',
        channelName:'',
      }
    },
    methods:{
      cancel:function(){
        this.$emit('isadd', true)
      },
      submit:function(){
        var token = sessionStorage.getItem('shiro-token')
        if(!numRe.test(this.schoolNumber)){
          this.showToast("编号只能输入数字")
          return false
        }
        if(!this.schoolName){
          this.showToast("请输入学校名称")
          return false
        }
         if(!this.channelName){
          this.showToast("请输入通道名称")
          return false
        }
        this.isReflash = true
        var that = this
        var instance = this.$axios.create({
            headers: {'Authorization':token}
        });
        instance.post('ganzi-server/school/addSchool',{
          schoolName:this.schoolName,
          schoolNumber:this.schoolNumber,
          information:this.information,
          channelName:this.channelName
        })
        .then(function (response) {
          that.showToast(response.data.msg)
          if(response.data.code == '200'){
            that.clear()
          }
          that.isReflash = false
        })
        .catch(function (error) {
          if(error.response && error.response.data.status == 4001){
              sessionStorage.clear();
              that.$router.push({path: '/'})
            }else if(error.response && error.response.data.status == 4002){
              sessionStorage.clear();
              that.$options.methods.showToast('您的账号在其他地方登录,如非本人操作，请立即修改密码！')
              that.$router.push({path: '/'})
            }else{
              that.$options.methods.showToast('系统异常!')
            }
          that.isReflash = false
        });
        return true
      },
      showToast:function(msg){
        $.alert({
          title: '消息!',
          content: msg,
          confirmButton: '确定',
          confirmButtonClass: 'btn-warning',
          backgroundDismiss: false,
          confirm: function(){
          }
        });
      },
      clear:function(){
        this.schoolName = ''
        this.information = ''
        this.schoolNumber = ''
        this.channelName = ''
      }
    }
  }
</script>

<style scoped>
  @import "../assets/css/school.css";
</style>
